function [ varargout ] = showDiverseTIs_REPs( varargin )
%SHOWMRI Summary of this function goes here
%   Detailed explanation goes here
gui_Singleton = 0;
gui_State = struct('gui_Name', mfilename, ...
                    'gui_Singleton', gui_Singleton, ...
                    'gui_OpeningFcn', @fig_OpeningFcn, ...
                    'gui_OutputFcn', @fig_OutputFcn, ...
                    'gui_LayoutFcn', [],...
                    'gui_Callback', []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end

end

function fig_OpeningFcn(obj,event,handles,varargin)
set(obj,'numbertitle','off');
set(obj,'name','Show Results of Diverse TIs and REPs');



if isempty(varargin)
%    handles.path = uigetdir('c:\users\lyu\Documents\MATLAB\Results\DiverseTI\');
%    handles.nREPs = 2:2:40;
%    handles.maxREPs = length(handles.nREPs);
%    handles.nTIs = 2:1:11;
%    handles.maxTIs = length(handles.nTIs);
%    handles.nSlice = 14;

   handles.path = uigetdir('c:\users\lyu\Documents\MATLAB\Results\DiverseTIREP_100\');
   handles.nREPs = 5:5:40;
   handles.maxREPs = length(handles.nREPs);
   handles.nTIs = 2:1:11;
   handles.maxTIs = length(handles.nTIs);
   handles.nSlice = 14;
end

%%%------------------
set(handles.slider1,'Min',1,...
                    'SliderStep',[1/(handles.nSlice-1),1/(handles.nSlice-1)],...
                    'Value',1,...
                    'Max',handles.nSlice);
set(handles.slider1,'Callback',@slideSlice);
handles.cSlice = round(get(handles.slider1,'Value'));
set(handles.slider1,'TooltipString',['Slice: ',num2str(handles.cSlice)]);

set(handles.slider2,'Min',handles.nTIs(1),...
                    'SliderStep',[1/(handles.maxTIs-1),1/(handles.maxTIs-1)],...
                    'Value',handles.nTIs(1),...
                    'Max',max(handles.nTIs));
set(handles.slider2,'Callback',@slideTIs);
handles.cTIs = round(get(handles.slider2,'Value'));
set(handles.slider2,'TooltipString',['TIs: ',num2str(handles.cTIs)]);



set(handles.slider3,'Min',handles.nREPs(1),...
                    'SliderStep',[1/(handles.maxREPs-1),1/(handles.maxREPs-1)],...
                    'Value',handles.nREPs(1),...
                    'Max',max(handles.nREPs));
set(handles.slider3,'Callback',@slideREPs);
handles.cREPs = round(get(handles.slider3,'Value'));
set(handles.slider3,'TooltipString',['REPs: ',num2str(handles.cREPs)]);

set(handles.popupmenu1,'Callback',@displayRes)
set(handles.popupmenu2,'Callback',@displayRes)

set(obj,'windowbuttonmotionfcn',@moveMousePoint);
set(obj,'windowbuttondownfcn',@selectVoxel);
set(handles.axes2,'buttondownfcn',@popoutShow);

guidata(obj,handles);
end


function slideSlice(src,event)
handles = guidata(src);
[slice,~,~] = readInfo(handles);
if ~isequal(slice,handles.cSlice)
    handles.cSlice = slice;
    set(handles.slider1,'TooltipString',['Slice: ',num2str(slice)]);
    displayRes(src,[]);
    guidata(src,handles);
end
end

function slideTIs(src,event)
handles = guidata(src);
[~,tis,~] = readInfo(handles);
if ~isequal(tis,handles.cTIs)
    handles.cTIs = tis;
    set(handles.slider2,'TooltipString',['TIs: ',num2str(tis)]);
    displayRes(src,[]);
    guidata(src,handles);
end
end

function slideREPs(src,event)
handles = guidata(src);
[~,~,reps] = readInfo(handles);
if ~isequal( reps, handles.cREPs)
    handles.cREPs = reps;
    set(handles.slider3,'TooltipString',['REPs: ',num2str(reps)]);
    displayRes(src,[]);
    guidata(src,handles);
end
end


function displayRes(src,event)
%%%%-------------------
%%%% Loading files
handles = guidata(src);
[slice,tis,reps,clow,chigh,isCBF,nIters] = readInfo(handles);
pushInfo(handles,slice,tis,reps,[],[],[],[]);
fileName = 'Divasl_CBF_AAT_Gray';
savedName = [fileName,...
            '_REPs',num2str(reps),...
            '_TIs',num2str(tis),...
            '_method_cmf'];
if ~isfield(handles,savedName)        
    savedFile = fullfile(handles.path,savedName);
    load(savedFile);
    handles = setfield(handles,savedName,obj);
    guidata(src,handles);
end

data = getfield(handles,savedName);
if isCBF
    if nIters
        data2Display = data(nIters).dCBF.dMRI(:,:,slice,1);
    else
        data2Display = data(1).dCBF.dMRI(:,:,slice,1)...
            +data(2).dCBF.dMRI(:,:,slice,1)...
            +data(3).dCBF.dMRI(:,:,slice,1)...
            +data(4).dCBF.dMRI(:,:,slice,1)...
            +data(5).dCBF.dMRI(:,:,slice,1);
        data2Display = data2Display / 5;
    end
else
    if nIters
        data2Display = data(nIters).dDT.dMRI(:,:,slice,1);
    else
        data2Display = data(1).dDT.dMRI(:,:,slice,1)...
            +data(2).dDT.dMRI(:,:,slice,1)...
            +data(3).dDT.dMRI(:,:,slice,1)...
            +data(4).dDT.dMRI(:,:,slice,1)...
            +data(5).dDT.dMRI(:,:,slice,1);
        data2Display = data2Display / 5;
    end
end
% data2Display = data(2).dCBF.dMRI(:,:,slice,1);
axes(handles.axes1);
imagesc(data2Display,[clow, chigh]);
colormap jet; axis off; axis equal;%axis image

edges = [clow:(chigh-clow)/100:chigh];
bar(handles.axes2,edges,histc(data2Display(abs(data2Display)>0),edges),'histc');axis('tight');
set(handles.axes2,'XLim',[clow,chigh]);

end

function [slice,tis,reps,clow,chigh,isCBF,nIters] = readInfo(handles)
slice = round(get(handles.slider1,'Value'));
tis = round(get(handles.slider2,'Value'));
reps = round(get(handles.slider3,'Value'));

clow = str2double(get(handles.edit1,'String'));
chigh = str2double(get(handles.edit2,'String'));

if nargout>5
    text = get(handles.popupmenu1,'Value');
    if isequal(text,1)
        isCBF = true;
    else
        isCBF = false;
    end
    
    nIters = get(handles.popupmenu2,'Value');
    if nIters == 6
        nIters = 0;
    end
end
end

function pushInfo(handles,slice,tis,reps,clow,chigh,isCBF,nIters)
set(handles.text1,'String',['Slice: ',num2str(slice)]);
set(handles.text2,'String',['TIs: ',num2str(tis)]);
set(handles.text3,'String',['REPs: ',num2str(reps)]);
end

function fig_OutputFcn(obj,event,handles)
end


function moveMousePoint(src,event)
handles = guidata(src);
position = get(handles.axes1,'CurrentPoint');


x = round(position(1));
y = round(position(3));
% axes(handles.axes1);
% title(['At point (',num2str(y),',',num2str(x),')']);
[slice,tis,reps,~,~,isCBF,nIters] = readInfo(handles);
fileName = 'Divasl_CBF_AAT_Gray';
savedName = [fileName,...
    '_REPs',num2str(reps),...
    '_TIs',num2str(tis),...
    '_method_cmf'];
if isfield(handles,savedName)
    data = getfield(handles,savedName);
else
    savedFile = fullfile(handles.path,savedName);
    load(savedFile);
    handles = setfield(handles,savedName,obj);
    guidata(src,handles);
%     return;
    data = getfield(handles,savedName);
end

if x<=64&&x>0&&y<=64&&y>0
    handles.x = x;
    handles.y = y;
    
    % disp(handles.y);
    if isCBF
        if nIters
            dataCurrent = data(nIters).dCBF.dMRI(:,:,slice,1);
        else
            dataCurrent = data(1).dCBF.dMRI(:,:,slice,1)...
                         +data(2).dCBF.dMRI(:,:,slice,1)...
                         +data(3).dCBF.dMRI(:,:,slice,1)...
                         +data(4).dCBF.dMRI(:,:,slice,1)...
                         +data(5).dCBF.dMRI(:,:,slice,1);
           dataCurrent = dataCurrent / 5;
        end
    else
        if nIters
            dataCurrent = data(nIters).dDT.dMRI(:,:,slice,1);
        else
            dataCurrent = data(1).dDT.dMRI(:,:,slice,1)...
                         +data(2).dDT.dMRI(:,:,slice,1)...
                         +data(3).dDT.dMRI(:,:,slice,1)...
                         +data(4).dDT.dMRI(:,:,slice,1)...
                         +data(5).dDT.dMRI(:,:,slice,1);
           dataCurrent = dataCurrent / 5;
        end
    end
    Infos = ['(',num2str(handles.x),',',num2str(handles.y),')',...
        '-->',num2str(dataCurrent(handles.y,handles.x))];
    % if src==1
    %     disp(Infos);
    % end
    set(handles.text6,'String',Infos);
end
end


function selectVoxel(src,event)
handles = guidata(src);
position = get(handles.axes1,'CurrentPoint');

% positionPop = get(handles.axes2,'CurrentPoint');


x = round(position(1));
y = round(position(3));
if x<=64&&x>0&&y<=64&&y>0
    handles.x = x;
    handles.y = y;
    guidata(src,handles);
    
    showERROR(src,handles,handles.axes2);
    
end
end

function showERROR(src,handles,handles_dis)
[slice,tis,reps,~,~,isCBF,nIters] = readInfo(handles);
    
    if ~isfield(handles,'pCBF_ERROR');
    dataCBFCurrent = zeros(64,64,handles.maxTIs,handles.maxREPs,6);
    dataDTCurrent = zeros(64,64,handles.maxTIs,handles.maxREPs,6);
    for nTIs = handles.nTIs
        for nREPs = handles.nREPs
            fileName = 'Divasl_CBF_AAT_Gray';
            savedName = [fileName,...
                '_REPs',num2str(nREPs),...
                '_TIs',num2str(nTIs),...
                '_method_cmf'];
%             if isfield(handles,savedName)
%                 data = getfield(handles,savedName);
%             else
                savedFile = fullfile(handles.path,savedName);
                load(savedFile);
                data = obj;
                %                 handles = setfield(handles,savedName,obj);
                %                 guidata(src,handles);
                %     return;
                %                 data = getfield(handles,savedName);
                %             end
                for n = 0:1:5
                if n
                    dataCBFCurrent(:,:,nTIs-1,nREPs/2,n+1) = data(n).dCBF.dMRI(:,:,slice,1);
                    dataDTCurrent(:,:,nTIs-1,nREPs/2,n+1) = data(n).dDT.dMRI(:,:,slice,1);
                else
                    dataCBFCurrent(:,:,nTIs-1,nREPs/2,n+1) = data(1).dCBF.dMRI(:,:,slice,1)...
                        +data(2).dCBF.dMRI(:,:,slice,1)...
                        +data(3).dCBF.dMRI(:,:,slice,1)...
                        +data(4).dCBF.dMRI(:,:,slice,1)...
                        +data(5).dCBF.dMRI(:,:,slice,1);
                    dataCBFCurrent(:,:,nTIs-1,nREPs/2,n+1) = dataCBFCurrent(:,:,nTIs-1,nREPs/2,n+1) / 5;
                    
                    dataDTCurrent(:,:,nTIs-1,nREPs/2,n+1) = data(1).dDT.dMRI(:,:,slice,1)...
                        +data(2).dDT.dMRI(:,:,slice,1)...
                        +data(3).dDT.dMRI(:,:,slice,1)...
                        +data(4).dDT.dMRI(:,:,slice,1)...
                        +data(5).dDT.dMRI(:,:,slice,1);
                    dataDTCurrent(:,:,nTIs-1,nREPs/2,n+1) = dataDTCurrent(:,:,nTIs-1,nREPs/2,n+1) / 5;
                end
                end
                
        end
    end

        pCBF_ERROR = dataCBFCurrent - repmat(dataCBFCurrent(:,:,nTIs-1,nREPs/2,:),[1,1,handles.maxTIs,handles.maxREPs,1]);
        handles.pCBF_ERROR = abs(pCBF_ERROR./(repmat(dataCBFCurrent(:,:,nTIs-1,nREPs/2,:),[1,1,handles.maxTIs,handles.maxREPs,1])+eps));
        pDT_ERROR = dataDTCurrent - repmat(dataDTCurrent(:,:,nTIs-1,nREPs/2,:),[1,1,handles.maxTIs,handles.maxREPs,1]);
        handles.pDT_ERROR = abs(pDT_ERROR./(repmat(dataDTCurrent(:,:,nTIs-1,nREPs/2,:),[1,1,handles.maxTIs,handles.maxREPs,1])+eps));
        guidata(src,handles);

    end
    
    if isCBF
        pERROR = handles.pCBF_ERROR(:,:,:,:,nIters+1);
    else
        pERROR = handles.pDT_ERROR(:,:,:,:,nIters+1);
    end
    
    if get(handles.checkbox1,'Value')
        pERROR = sum(sum(pERROR,1),2)/64/64;
        data2Dis = reshape(pERROR,handles.maxTIs,handles.maxREPs);
    else
        pERROR = double(pERROR );
        data2Dis = reshape(pERROR(handles.y,handles.x,:,:),handles.maxTIs,handles.maxREPs);
    end
    axes(handles_dis);
   surf(handles.nREPs,handles.nTIs,data2Dis);axis tight;
    view(handles_dis,119,36);
    disp(get(handles.text6,'String'));
end

function popoutShow(src,event)
% posAxes2 = get(handles.axes2,'CurrentPoint');
% if ([40,10]-abs(posAxes2(1,1:2)))>=0
%     disp(posAxes2);
    handles = guidata(src);
    figure(100);clf;
    handles_axes = axes;
    showERROR(src,handles,handles_axes);
    
%     return;
end

% handles = guidata(src);
% h = figure;
% showERROR(src,handles,h);
% end